Functions¶

No description has been provided for this image

NOTES

A woman and her husband were standing in the Lourve observing the Mono Lisa. The husband turned to his wife and commented: "I don't see what so great about this painting." In response, the musuem guard standing nearby leaned over and replied: "Ah! But don't you wish you could!"

The ability to behold beauty, creativity, and talent is a gift. In a domain that is new to us, we may sometimes feel like the husband in this story: failing to appreciate what is in front of us.

At first, you may have a similar experience as you learn how to program. There are many ways to write a program, but some are better than others. You might not appreciate the intelligence behind the differences at first, but as you practice and study, the differences will become more clear. You will learn the patterns, intuitions, and techniques that make programming enjoyable and powerful.

Then, not only will you be able to appreciate the competent work of others, you will be able to create intelligent, beautiful code of your own.

Writing code is like painting.

There are techniques to learn, like color-mixing, brush strokes, layering, shading, perspective, etc.

There is also creative intuition: the ability to exercise the techniques you know to create something beautify.

To build beautiful code, you'll need to know the techniques, and you'll need to develop creative intuition.

Over the next few days, we'll talk about important techniques and practice, practice, practice!

def¶

You can define more than one function in a script.

three_reds.py¶

snapshot¶

Bit has the ability to take a snapshot of the world using the snapshot function.

snap_three_reds.py¶

You can use any phrase you want when making a snapshot.

The Jump buttons take you to the previous or next snapshot.

Naming variables¶

cosmo.py¶

When naming a function or variable:

  • Use underscore _ (next to the zero key + shift) to break up compound names
    • go_green instead of gogreen or goGreen
  • use lower-case characters
    • go instead of Go or GO
    • Casing matters: go is different from Go!

In blue_squares.py, when will Bit paint a blue square?

blue_squares.py¶

Defining a function is not the same as calling a function.

Docstrings¶

The very first line after def can have a string. When this is the case, we call that string a docstring because it documents the function: you use it to describe what the function is for and how to use it.

Strings that use single quotation characters (e.g. " or ') are only one line.

If you want to use multiple lines for your string, use triple quotes (""" or ''').

No description has been provided for this image

NOTES

Gordon Bean:

When our oldest child was just learning how to crawl, my wife and I discovered that our daughter would often get right up behind us without us realizing it. When we would turn around to move to another activity, we would stumble over her. It wasn't a great setup. We were often warning each other that there was a baby right behind. So one day my wife declared: "We need a code word we can use to say 'be careful turning around: there is a baby right behind you'" I agreed that was a good idea, and after a moment's thought she declared "jellyfish".

So, for several years, we've used the term jellyfish to communicate "watch out, there is someone/something right behind you that you might run into as you turn around". It's become second nature. So much, in fact, that I almost used it with a colleague one day as I navigated a crowded room and was passing right behind her.

Human beings have a natural ability for creating ideas ("watch out for the baby right behind you") and giving those ideas names ("jellyfish").

Defining functions is just like that. We get to come up with the idea we want and name it. Then we can use it just like it is an ordinary word.

Functions give you the ability to create and name new ideas.

Smiles 😁 😃 🙂 🙃¶

Given the following starting world:

No description has been provided for this image

Draw four blue smiles:

No description has been provided for this image

smiles.py¶

  • Draw out a strategy
  • Identify the pieces
    • What are the ideas, how do they fit together?
  • Implement one piece at a time

Key Ideas¶

  • Define functions with def
  • Make functions to represent complex ideas: abstraction
  • Make functions to define tasks that will be repeated
  • Naming functions and variables
  • Docstrings